package cn.kgc.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * session作用域的监听器       单端登录  session   user  -> session
 * 1.生命周期   session创建  销毁的过程
 * 2.监听域中值得改变  值得添加 修改  删除
 * @author: mosin
 * @version: v1.0  2022/3/22
 */
@WebListener //标记当前类是一个监听器
public class MySessionListener implements HttpSessionListener , HttpSessionAttributeListener {
    /**
     * session创建
     * @param httpSessionEvent
     */
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("一个session被创建");
    }

    /**
     * session的销毁
     * @param httpSessionEvent
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        System.out.println("一个session被销毁");
    }

    //session 中添加值
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("session中有值被添加");
    }

    //session中值被移除
    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("session中的值被移除");
    }

    //session 中值被替换   session.setAttribute("username","lisi") session.setAttribute("username","zs")
    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("session中的值被替换");
    }
}
